﻿class ButtonEvent {
	// auxiliary to not to get a compiler error.
	private var dispatchEvent, ButtonEvent_onRollOver, ButtonEvent_onRollOut,
	ButtonEvent_onRelease, ButtonEvent_onReleaseOutside,
	ButtonEvent_onPress:Function;
	
	public static function aplicar(receptor:MovieClip):MovieClip {
		// temp is an auxiliary to not to get a compiler error on some compilers that forces to asign new objects to a variable.
		var temp:ButtonEvent = new ButtonEvent(receptor);
		return receptor;
	}

	public function ButtonEvent(receptor:MovieClip) {
		EventDispatcherWrapper.wrap(receptor);
		// Track original methods.
		receptor.ButtonEvent_onRollOver = receptor.onRollOver;
		receptor.ButtonEvent_onRollOut = receptor.onRollOut;
		receptor.ButtonEvent_onRelease = receptor.onRelease;
		receptor.ButtonEvent_onReleaseOutside = receptor.onReleaseOutside;
		receptor.ButtonEvent_onPress = receptor.onPress;
		
		receptor.onRollOver = rollOver_2;
		receptor.onRollOut = rollOut_2;
		receptor.onRelease = release_2;
		receptor.onReleaseOutside = releaseOutside_2;
		receptor.onPress = press_2;
	}

	private function rollOver_2():Void {
		//trace("onRollOver: " + this["ref"].dispatchEvent);
		//trace("onRollOver: " + (this["ButtonEventDispatcher_ref"] instanceof ButtonEventDispatcherWrapper));
		ButtonEvent_onRollOver();
		dispatchEvent({target: this, type: "rollOver"});
	}

	public function rollOut_2():Void {
		ButtonEvent_onRollOut();
		this.dispatchEvent({target: this, type: "rollOut"});
	}
	
	public function release_2():Void {
		ButtonEvent_onRelease();
		this.dispatchEvent({target: this, type: "release"});
	}
	
	public function releaseOutside_2():Void {
		ButtonEvent_onReleaseOutside();
		this.dispatchEvent({target: this, type: "releaseOutside"});
	}
	
	public function press_2():Void {
		ButtonEvent_onPress();
		this.dispatchEvent({target: this, type: "press"});
	}
}